package org.ll.demo25;

import io.micrometer.common.util.StringUtils;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session){
        if(StringUtils.isEmpty(userName)||StringUtils.isEmpty(password)){
            return false;
        }
        //判断账号密码是否正确
        //TODO 当账号为admin，密码为123456 正确 之后连数据库判断
        //"admin".equals()不会出现空指针异常
        if("admin".equals(userName)&&"admin".equals(password)){
            session.setAttribute("userName",userName);
            return true;
        }
        return false;
    }
    @RequestMapping("/getUserName")
    public String getUserName(HttpSession session){
        String userName = (String) session.getAttribute("userName");
        if(StringUtils.isEmpty(userName)){
            return "";
        }
        return userName;
    }
}
